package com.GlobalPaint.app.ui.Mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.GlobalPaint.app.Base.BaseFragment;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.listener.UILPauseOnScrollListener;
import com.GlobalPaint.app.ui.Login.Login;
import com.GlobalPaint.app.ui.Mine.Personal.PersonalActivity;
import com.GlobalPaint.app.ui.Mine.setting.SettingActivity;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.DataManager;
import com.GlobalPaint.app.utils.UILImageLoader;
import com.blankj.utilcode.utils.FileUtils;
import com.blankj.utilcode.utils.SPUtils;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;

import java.util.ArrayList;
import java.util.List;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import de.hdodenhof.circleimageview.CircleImageView;


public class MineFragment extends BaseFragment implements View.OnClickListener{
    private final int REQUEST_CODE_CAMERA = 1000;
    private final int REQUEST_CODE_GALLERY = 1001;
    private List<PhotoInfo> mDatas = new ArrayList<>();
    private CircleImageView imge_touxiang;
    private TextView bt_tuichu;
    private RelativeLayout rl_setting;
    private RelativeLayout rl_resume;
    private RelativeLayout rl_information;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_mine, container, false);
            initView(view);
            return view;

    }

    private void initView(View view) {
        imge_touxiang = (CircleImageView)view. findViewById(R.id.imge_touxiang);
        imge_touxiang.setOnClickListener(this);
        bt_tuichu = (TextView)view. findViewById(R.id.bt_tuichu);
        bt_tuichu.setOnClickListener(this);
        rl_setting = (RelativeLayout) view.findViewById(R.id.rl_setting);
        rl_setting.setOnClickListener(this);
        rl_resume = (RelativeLayout) view.findViewById(R.id.rl_resume);
        rl_resume.setOnClickListener(this);
        rl_information = (RelativeLayout)view.findViewById(R.id.rl_information);
        rl_information.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.imge_touxiang:
                selectImg();
                break;
            case R.id.rl_resume:
                Intent Intent3=new Intent(getContext(), ResumeActivity.class);
                Intent3.putExtra("activity","MineFragment");
                startActivity(Intent3);
                break;
            case R.id.rl_setting:
                Intent Intent2=new Intent(getContext(), SettingActivity.class);
                startActivity(Intent2);
                break;
            case R.id.bt_tuichu:
                SPUtils spUtils = new SPUtils(DataManager.context, "login");
                spUtils.remove("user");
                //退出时清除缓存
                DataManager.lruCache = new LruCache<>(10);
                Intent intent = new Intent(getActivity(), Login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().finish();
                break;
            case R.id.rl_information:
                Intent Intent1=new Intent(getContext(), PersonalActivity.class);
                startActivity(Intent1);
                break;

        }
    }
    //打开相册
    private void selectImg() {
        mDatas.clear();
        ThemeConfig theme = new ThemeConfig.Builder()
                .build();
        //配置功能
        final FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(false)
                .setEnableCrop(true)
                .setEnableRotate(true)
                .setCropSquare(true)
                .setEnablePreview(true)
                .setSelected(mDatas)
                .setMutiSelectMaxSize(1)
//                .setFilter(mList)
                .build();
        CoreConfig coreConfig = new CoreConfig.Builder(getContext(), new UILImageLoader(), theme)
                .setFunctionConfig(functionConfig)
                .setPauseOnScrollListener(new UILPauseOnScrollListener(false, true))
                .build();
        GalleryFinal.init(coreConfig);
        String[] function = new String[]{"打开相册", "拍照"};
        final ActionSheetDialog dialog = new ActionSheetDialog(getContext(), function, null);
        dialog.isTitleShow(false).show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, functionConfig, mOnHanlderResultCallback);
                        break;
                    case 1:
                        GalleryFinal.openCamera(REQUEST_CODE_CAMERA, functionConfig, mOnHanlderResultCallback);
                        break;
                    default:
                        break;
                }
                dialog.dismiss();
            }
        });
    }
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            if (resultList != null) {

                for (PhotoInfo result : resultList) {
                    boolean exists = false;
                    for (PhotoInfo photoInfo : mDatas) {
                        if (TextUtils.equals(photoInfo.getPhotoPath(), result.getPhotoPath())) {
                            exists = true;
                            break;
                        }
                    }
                    if (!exists) {
                        boolean deal = CommUtility.dealRotateImage(result.getPhotoPath());
                        mDatas.add(result);
                    }
                }
                if (FileUtils.isFileExists(resultList.get(0).getPhotoPath())) {

//                        String filePath = SiliCompressor.with(getApplicationContext()).compress(mDatas.get(0).getPhotoPath());
//                        ImageLoader.getInstance().displayImage(filePath,imge_touxiang);
                 /*   try {
                        imge_touxiang.setImageBitmap(SiliCompressor.with(PersonalInformation.this).getCompressBitmap(mDatas.get(0).getPhotoPath()));
                        //上传oss
                        getNetworkData();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/


                }
            }
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {
            CommUtility.ShowMsgShort(getContext(), errorMsg);
        }
    };
}
